package com.tarena.sentinel.demo01.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.tarena.sentinel.demo01.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    public String sayHi(String name){
        Entry entry=null;
        String result=null;
        try {
            entry = SphU.entry("resource01");
            result=helloService.sayHi(name);
        }catch (BlockException e){
            return "你的请求被流量拦截了";
        }finally {
            if (entry!=null)entry.exit();
        }
        return result;
    }
}
